import router from '../router'
import store from '~/store/index'

// token名字
export const ADMIN_TOKEN = 'admin-token'

export let firstRouter = null

// 注册本地路由 data为数组
export function addLocalRouter(data) {
  let localRouter = []
  const components = import.meta.glob('~/pages/**/*.vue')
  
  // 遍历得到路由
  for (let i = 0; i < data.length; i++) {
    if(data[i].select) {
      const code = data[i].code
      for (let j = 0; j < data[i].children.length; j++) {
        if(data[i].children[j].select || data[i].children[j].isHidden) {
          const childrenCode = data[i].children[j].code
          let importPath = '/src/pages/' + code + '/' + childrenCode + '.vue'
          const component = components[importPath]
          const name = childrenCode
          let datas = {
            path: '/' + code + '/' + childrenCode,
            component,
            name,
            id: data[i].children[j].id,
            hidden: data[i].children[j].isHidden // 是否出现在侧边栏，false会出现，true不会出现，默认false
          }
          localRouter.push(datas)
        }
      }
    }
  }
  for (const route of localRouter) {
    if(!firstRouter) firstRouter = route
    router.addRoute('index', route)
  }
}

/**
 * 根据当前的路径得到当前刷新的是哪个菜单
 * @param {string} path 当前路径
 * @param {Array} menus 所有的菜单集合
 */
export function mapPathToMenu(path, menus) {
  for(const menu of menus) {
    const parentCode = menu.code
    for(const children of menu.children) {
      const childrenCode = children.code
      let url = '/' + parentCode + '/' + childrenCode
      if(url === path) {
        return children
      }
    }
  }
}

/**
 * 根据当前的路径得到当前的面包屑
 * @param {string} path 当前路径
 * @param {Array} menus 所有的菜单集合
 */
export function mapPathToBreadcrumbs(path, menus) {
  const breadCrumbs = []
  for(const menu of menus) {
    const parentCode = menu.code
    for(const children of menu.children) {
      const childrenCode = children.code
      let url = '/' + parentCode + '/' + childrenCode
      if(url === path) {
        breadCrumbs.push({ lable: menu.name, name: menu.children[0].code, path: '/' + parentCode + '/' + menu.children[0].code })
        store.commit('UPDATE_TAGS', {
          lable: children.name, 
          name: childrenCode, 
          path: url
        })
        breadCrumbs.push({ lable: children.name, name: childrenCode, path: url })
        return breadCrumbs
      }
    }
  }
}

/**
 * 根据菜单列表得到当前用户的按钮权限标识
 * @param {Array} menuList 菜单列表
 */
export function mapMenusToPermissions(menuList) {
  const permissions = []

  function recurseGetPermission(menus) {
    for(const item of menus) {
      if(item.type === 3 && item.select) {
        permissions.push(item.code)
      } else {
        recurseGetPermission(item.children ?? [])
      }
    }
  }
  recurseGetPermission(menuList)

  return permissions
}

// 删除数组中重复的数据
export function removeDuplicates(arr) {
  const uniqueObjects = [];
  const seen = new Set();

  arr.forEach((item) => {
    const stringifiedItem = JSON.stringify(item);
    if (!seen.has(stringifiedItem)) {
      uniqueObjects.push(item);
      seen.add(stringifiedItem);
    }
  });

  return uniqueObjects;
}
